package de.tum.in.eist.im.client;

import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

import de.tum.in.eist.im.client.domain.User;
/**
 * Class that sets up the LogOut Funtionaliy and the UserList.
 * @author Stephan
 *
 */
public class ChatList {
	//Panel that shows the user logged in and provides a logout button
	final HorizontalPanel logout = new HorizontalPanel();
	final Button logoutButton = new Button("Logout");
	final Label userLabel = new Label();
	final EIST_IM client;
	final ChatServiceAsync chatService;
	// Panel for the logout Panel, the userListTable
	final VerticalPanel chatList = new VerticalPanel();
	final FlexTable userListTable = new FlexTable();
	
	final ScrollPanel scr = new ScrollPanel();
	
	/**
	 * creates the chatList with ClickHandler for logoutButton
	 * @param chatS
	 * @param c
	 */
	public ChatList(ChatServiceAsync chatS, EIST_IM c){
		logout.add(userLabel);
		userLabel.setStyleName("userLabel");
		logout.add(logoutButton);
		logoutButton.setStyleName("logout");
		logoutButton.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event){
				logout();
			}
		});
		chatList.add(logout);
		chatList.add(scr);
		scr.setStyleName("scr");
		scr.add(userListTable);
		userListTable.addStyleName("userList");
		client = c;
		chatService = chatS;
	}
	public void setUser(User u){
		userLabel.setText(u.getUsername());
	}
	public VerticalPanel getVPanel (){
		return chatList;
	}
	/**
	 * fill in the users into the Flextable userListTable with only one column.
	 * @param list
	 */
	public void setLoggedInUsers(List<User> list){
		userListTable.removeAllRows();
		while(!list.isEmpty()) {
			userListTable.setText(userListTable.getRowCount(), 0, list.remove(0).getUsername());
		}
	}
	public void setLoggedInUser(User u){
		userListTable.setText(userListTable.getRowCount(), 0, u.getUsername());
	}
	/**
	 * logs out the user and invokes the clients reload-method.
	 */
	public void logout (){
		chatService.logout(client.getUser(), new AsyncCallback<Boolean>(){

			@Override
			public void onFailure(Throwable caught) {
				client.setErrorLabelText("failed to log out, try again");	
			}

			@Override
			public void onSuccess(Boolean result) {
				client.reload();
			}
			
		});
	}
	/**
	 * removes a user from the FlexTable userListTable.
	 * @param u
	 */
	public void remove(User u){
		int row = userListTable.getRowCount();
		for (int i = 0; i<row; i++){
			if(userListTable.getText(row, 0) == u.getUsername()){
				userListTable.removeRow(i);
			}
		}
	}
}
